package com.ljx.vod.service.impl;


import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ljx.model.vod.Subject;
import com.ljx.vo.vod.SubjectEeVo;
import com.ljx.vod.mapper.SubjectMapper;
import com.ljx.vod.service.SubjectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ljx.vod.utils.SubjectListener;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author ljx
 * @since 2023-04-04
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject> implements SubjectService {

    @Autowired
    private SubjectListener subjectListener;

    /**
     * 懒加载，每次查询一层数据
     * 根据parent_id查询
     * @param id
     * @return
     */
    @Override
    public List<Subject> selectSubjectList(Long id) {
        QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        List<Subject> lists = baseMapper.selectList(queryWrapper);
        List<Subject> collect = lists.stream().map(list -> {
            Long subjectId = list.getId();
            boolean isChild = this.isChildren(subjectId);
            list.setHasChildren(isChild);
            return list;
        }).collect(Collectors.toList());
        return collect;
    }

    /**
     * 导出课表
     * @param response
     */
    @Override
    public void exportData(HttpServletResponse response) {

        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
            String fileName = URLEncoder.encode("课程分类", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename="+ fileName + ".xlsx");
            List<Subject> dictList = baseMapper.selectList(null);
            List<SubjectEeVo> dictVoList = dictList.stream().map(sub -> {
                SubjectEeVo subjectEeVo = new SubjectEeVo();
                BeanUtils.copyProperties(sub, subjectEeVo);
                return subjectEeVo;
            }).collect(Collectors.toList());
            EasyExcel.write(response.getOutputStream(), SubjectEeVo.class)
                    .sheet("课程分类").doWrite(dictVoList);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * 导入课程
     * @param file
     */
    @Override
    public void importDictData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),
                    SubjectEeVo.class,subjectListener).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 判断是否有下一级结果
     * @param subjectId
     * @return
     */
    private boolean isChildren(Long subjectId) {
        QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id", subjectId);
        Integer count = baseMapper.selectCount(queryWrapper);
        return count > 0;
    }
}
